package com.xxx.controller;

import com.xxx.common.AppResult;
import com.xxx.common.ResultCode;
import com.xxx.exception.ApplicationException;
import com.xxx.model.Board;
import com.xxx.services.impl.BoardServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
@RequestMapping("/board")
@Api(tags = "板块接口")
@RestController
@Slf4j
public class BoardController {
    //从配置文件中读值, 如果找不到, 默认9
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;
    @Autowired
    private BoardServiceImpl boardService;
    /**
     * 查询首页板块列表
     * @return
     */
    @ApiOperation("获取板块首页列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        log.info("首页板块个数为:" + indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards == null){
            boards = new ArrayList<>();
        }

        return AppResult.success(boards);

    }
    @ApiOperation("获取板块信息")
    @GetMapping("getById")
    public AppResult<Board> getById(@ApiParam("板块Id") @RequestParam("id") @NonNull Long id){
        Board board = boardService.selectById(id);
        if(board == null || board.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        return AppResult.success(board);
    }
}
